var WorkTimeSetting = function () {
  var template = /* html */ `
    <div class="work-time-setting">
      <div>
        <el-button type="primary" size="medium" @click="addWorkTime">添加工作时间</el-button>
        <el-button type="primary" size="medium" @click="ensure">确定</el-button>
      </div>
      <div style="margin-top:10px;">
        <div v-for="(workTime,index) in workTimeList" :key="workTime.id" class="work-time-item">
          <time-range-picker v-model="workTime.timeRange" start-placeholder="开始时间" end-placeholder="结束时间" format="HH:mm" size="medium"
            :editable="false" :clearable="false" :default-value="workTime.defaultValue" :selectable-range="getSelectableRange(index)"></time-range-picker>
          <span style="margin-left:5px">
            <el-button class="delete-btn" type="text" icon="el-icon-delete" title="删除" @click="delWorkTime(index)"></el-button>
          </span>
        </div>
      </div>
    </div>
  `;

  return {
    props: {
      equipment: {
        type: Object,
        required: true
      }
    },

    data: function () {
      return {
        workTimeList: this.getWorkTimeList()
      };
    },

    methods: {
      getWorkTimeList: function () {
        var workTimeList = this.equipment.workTimeList;
        if (workTimeList.length === 0) {
          return [];
        }

        workTimeList = _.map(workTimeList, function (workTime) {
          var startTime = workTime.startTime;
          var endTime = workTime.endTime;
          return {
            id: SysUtil.getId(),
            timeRange: [startTime, endTime]
          }
        });
        return workTimeList;
      },

      getSelectableRange: function (index) {
        var workTimeList = this.workTimeList;
        var startTime, endTime;
        var preWorkTime = workTimeList[index - 1];
        if (preWorkTime === undefined) {
          startTime = this.getTime(0);
        } else {
          startTime = preWorkTime.timeRange[1];
        }

        var nextWorkTime = workTimeList[index + 1];
        if (nextWorkTime === undefined) {
          endTime = this.getTime(23, 59);
        } else {
          endTime = nextWorkTime.timeRange[0];
        }
        return [startTime, endTime];
      },

      getTime: function (hour, minute) {
        if (hour === undefined) {
          hour = 0;
        }
        if (minute === undefined) {
          minute = 0;
        }
        var timeMoment = moment().startOf('day');
        timeMoment.set({
          hour: hour,
          minute: minute
        });
        return timeMoment.valueOf();
      },

      addWorkTime: function () {
        var workTimeList = this.workTimeList;
        var lastWorkTime = workTimeList[workTimeList.length - 1];
        if (lastWorkTime === undefined) {
          workTimeList.push({
            id: SysUtil.getId(),
            timeRange: [null, null],
            defaultValue: this.getTime()
          });
          return;
        }
        var timeRange = lastWorkTime.timeRange;
        if (timeRange[0] === null) {
          this.$alert('请输入开始时间', '信息')
          return;
        }
        if (timeRange[1] === null) {
          this.$alert('请输入结束时间', '信息')
          return;
        }
        workTimeList.push({
          id: SysUtil.getId(),
          timeRange: [null, null],
          defaultValue: timeRange[1]
        });
      },

      delWorkTime: function (index) {
        var workTimeList = this.workTimeList;
        workTimeList.splice(index, 1);
      },

      ensure: function () {
        var workTimeList = this.workTimeList;
        var workTimes = [];
        for (var i = 0; i < workTimeList.length; i++) {
          var timeRange = workTimeList[i].timeRange;
          var startTime = timeRange[0];
          if (startTime === null) {
            this.$alert('请输入开始时间', '信息')
            return;
          }

          var endTime = timeRange[1];
          if (endTime === null) {
            this.$alert('请输入结束时间', '信息')
            return;
          }
          workTimes.push({
            startTime: startTime,
            endTime: endTime
          });
        }
        this.equipment.workTimeList = workTimes;
        this.$emit('ensure', workTimes);
        this.$emit('close');
      }
    },

    components: {
      TimeRangePicker: TimeRangePicker
    },

    template: template
  };
}();